package com.xieyong.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Leetcode1695 {
    public static void main(String[] args) {
        Leetcode1695 leetcode1695 = new Leetcode1695();
        int[] nums = {558,508,782,32,187,103,370,607,619,267,984,10};
        int i = leetcode1695.maximumUniqueSubarray(nums);
        System.out.println(i);
    }


    public int maximumUniqueSubarray(int[] nums) {
        int n = nums.length;
        int[] presum = new int[n + 1];
        Map<Integer,Integer> map = new HashMap<>();
        int preIndex = 0;
        int res = 0;
        for (int i = 0; i < n; i++) {
            presum[i + 1] = presum[i] + nums[i];
            preIndex = Math.max(preIndex, map.getOrDefault(nums[i],0));
            map.put(nums[i], i + 1);
            res = Math.max(res, presum[i + 1] - presum[preIndex]);
        }
        return res;
    }
}
